﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows.Controls;
using GameSPK.Model;
using GameSPK.DAO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GameSPK.Helpers;
using System.Collections.ObjectModel;
using System.Windows;


namespace GameSPK.Start
{
    class frmModau: frmGenerel
    {
        private static frmModau instance;
        private static object syncLock = new object();
        public static frmModau getInstance()
        {
            if (instance == null)
            {
                lock (syncLock)
                {
                    if (instance == null)
                    {
                        instance = new frmModau();
                    }
                }
            }

            return instance;
        }
        private frmModau()
            : base()
        {
            this.Closed += new EventHandler(frmModau_Closed);
            this.Loaded += new System.Windows.RoutedEventHandler(frmModau_Loaded);
            AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)frmModau_KeyDown);
            
        }

        void frmModau_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            General cuocthichon = MainWindow.getCuocThiChon();
            lstNguoiChoi = NguoiChoiDAO.getNguoiChoiDaChon(cuocthichon);
            CuocThiDAO.setCuocThiStart(cuocthichon);
            songuoichoi = lstNguoiChoi.Count;
            GeneraLayout();
            
        }
        Button btnNext;
        ObservableCollection<INguoiChoi> lstNguoiChoi;
        List<Label> lstHienNguoiChoi; 
        int songuoichoi;
        public void GeneraLayout()
        {
            btnNext = new Button();
            LabelLayout buttonNextLayout = NguoiChoiXMLDAO.getButtonNext();
            btnNext.Margin = new System.Windows.Thickness(buttonNextLayout.Left, buttonNextLayout.Top,0,0);
            btnNext.Width = buttonNextLayout.Width;
            btnNext.Height = buttonNextLayout.Height;
            //btnNext.Content = "button";
            ImageBrush myBrush = new ImageBrush();
            myBrush.ImageSource =
                   new BitmapImage(new Uri(ConfigXMLDAO.getNextButtonUrl(), UriKind.RelativeOrAbsolute));
            //this.Background = myBrush;
            btnNext.Background = myBrush;
            btnNext.Visibility = System.Windows.Visibility.Visible;
            btnNext.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            btnNext.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                
            btnNext.Name = "btnNext";
            btnNext.Click += new RoutedEventHandler(btnNext_Click);
            LabelLayout nguoichoichung = NguoiChoiXMLDAO.getNguoiChoiChung();
            lstHienNguoiChoi = new List<Label>();
            for (int i = 0; i < songuoichoi; i++)
            {
                LabelLayout labelNguoichoi = NguoiChoiXMLDAO.getNguoiChoii(i);
                labelNguoichoi.Width = nguoichoichung.Width;
                labelNguoichoi.Height = nguoichoichung.Height;
                Label lblNguoiChoi = new Label();
                lblNguoiChoi.Name = "nguoichoi" + i;
                lblNguoiChoi.Visibility = System.Windows.Visibility.Visible;
                lblNguoiChoi.Margin = new System.Windows.Thickness(labelNguoichoi.Left, labelNguoichoi.Top, 0, 0);
                lblNguoiChoi.Width = labelNguoichoi.Width;
                lblNguoiChoi.Height = labelNguoichoi.Height;
                lblNguoiChoi.Content = lstNguoiChoi[i].TenNguoiChoi.Trim();
                DrawingBrush myDrawingBrush = new DrawingBrush();
                GeometryDrawing myGeometryDrawing = new GeometryDrawing();
                myGeometryDrawing.Brush = labelNguoichoi.Background;
                
                myGeometryDrawing.Pen = new Pen(Brushes.Red, 1);
                GeometryGroup ellipses = new GeometryGroup();
                ellipses.Children.Add(new EllipseGeometry(new Point(25, 50), 12.5, 50));
                myGeometryDrawing.Geometry = ellipses;
                myDrawingBrush.Drawing = myGeometryDrawing;
                lblNguoiChoi.Background = myDrawingBrush;


                //lblNguoiChoi.Background = labelNguoichoi.Background;
                lblNguoiChoi.Foreground = labelNguoichoi.Foreground;
                lblNguoiChoi.FontSize = labelNguoichoi.FontSize;
                lblNguoiChoi.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
                lblNguoiChoi.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                lblNguoiChoi.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                lblNguoiChoi.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                //lblNguoiChoi.FontFamily = new FontFamily("Tahoma");
                lblNguoiChoi.FontWeight = System.Windows.FontWeights.Bold;
                lblNguoiChoi.Style = (Style)this.FindResource("PulseLabel");
                lblNguoiChoi.MouseDoubleClick += new MouseButtonEventHandler(lblNguoiChoi_MouseDoubleClick);
                lstHienNguoiChoi.Add(lblNguoiChoi);
                gridMain.Children.Add(lblNguoiChoi);
                    
                    
            }


            this.gridMain.Children.Add(btnNext);
        }

        void btnNext_Click(object sender, RoutedEventArgs e)
        {
            frmGioiThieuPhanThi gioithieuphanthi = frmGioiThieuPhanThi.getInstance();
            gioithieuphanthi.ShowDialog();
        }

        void lblNguoiChoi_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Label nguoichoilbl = (Label)sender;
            string number = nguoichoilbl.Name;
            number = number.Replace("nguoichoi", "");
            frmGioiThieuNguoiChoi gioithieu = frmGioiThieuNguoiChoi.getInstance(lstNguoiChoi[int.Parse(number)]);
            gioithieu.ShowDialog();
        }
        void frmModau_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
        }
        void frmModau_Closed(object sender, EventArgs e)
        {
            instance = null;
        }
    }
}
